package logic

import (
	"context"
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/pkg"
	"strconv"

	"doctor-medicine-system/api/internal/svc"
	"doctor-medicine-system/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DoctorLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDoctorLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DoctorLoginLogic {
	return &DoctorLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DoctorLoginLogic) DoctorLogin(req *types.DoctorLoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	login, err := l.svcCtx.DoctorRpc.DoctorLogin(l.ctx, &doctorrpc.DoctorLoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}

	handler, err := pkg.TokenHandler(strconv.FormatInt(login.DoctorId, 10))
	if err != nil {
		return nil, err
	}

	return &types.Response{
		Message: "登录成功",
		Code:    200,
		Data:    handler,
	}, nil
}
